package com.bigdata.movieticketapi.service.impl;

import com.bigdata.movieticketapi.common.exception.BusinessException;
import com.bigdata.movieticketapi.common.exception.ResponseCode;
import com.bigdata.movieticketapi.entity.Movie;
import com.bigdata.movieticketapi.mapper.MovieMapper;
import com.bigdata.movieticketapi.service.IMovieService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigdata.movieticketapi.vo.req.MovieReqVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author movie
 * @since 2023-10-26
 */
@Service
public class MovieServiceImpl extends ServiceImpl<MovieMapper, Movie> implements IMovieService {

    @Resource
    private MovieMapper movieMapper;

    @Override
    public void addMovie(MovieReqVO vo) {
        Movie movie = query().select("id").last("limit 1").eq("chinese_name", vo.getChineseName()).one();
        if(movie != null){
            throw new BusinessException(ResponseCode.MOVIE_ALREADY_EXISTS.getCode(), ResponseCode.MOVIE_ALREADY_EXISTS.getMessage());
        }
        Movie insertMovie = new Movie();
        BeanUtils.copyProperties(vo,insertMovie);
        int result = movieMapper.insert(insertMovie);
        if (result !=1){
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(),ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public void updateMovie(Movie movie) {
        int result = movieMapper.updateById(movie);
        if (result !=1){
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(),ResponseCode.OPERATION_ERROR.getMessage());
        }
    }
}
